import React, { ReactElement, useEffect, useState } from 'react'
import { PictureOutlined } from '@ant-design/icons/lib'
import { ContentUtils } from 'braft-utils'
import BraftEditor from 'braft-editor'
import { Button, Upload } from 'antd'
import 'braft-editor/dist/index.css'

/*import { simpleUpload } from 'MultipartUploadService'
import { UPLOAD } from 'constants/api'*/

export default function TextEditPage (): ReactElement {

    // 设置编辑器初始内容
    const [editorState, setEditorState,simpleUpload,UPLOAD] = useState(BraftEditor.createEditorState(''))

    // 编辑内容触发
    const handleChange = (editorState: any): void => {
        setEditorState(editorState)
    }

    // 上传图片获取URL，此图片是上传（阿里云）之后拿地址放到编辑器内容里的
    const uploadHandler = (param: any): any => {

        if (!param.file) {
            return false
        }
        simpleUpload(
            UPLOAD,
            param.file,
            (result: any) => {
                const { code, data } = result
                if(code === 0) {
                    setEditorState(ContentUtils.insertMedias(editorState, [{
                        type: 'IMAGE',
                        url: data.url
                    }]))
                }
            }
        )
    }
    const extendControls = ['bold', 'italic', 'underline', 'text-color', 'separator', 'link',
        {
            key: 'antd-uploader',
            type: 'component',
            component: (
                <Upload
                    accept='image/*'
                    showUploadList={false}
                    customRequest={uploadHandler}
                >
                    <Button className='control-item button upload-button' data-title='插入图片'>
                        <PictureOutlined />
                    </Button>
                </Upload>
            )
        }]
    return <BraftEditor
        contentStyle={{ height:305 }}
        value={editorState}
        excludeControls={['media', 'emoji']}
        extendControls={extendControls}
        onChange={handleChange}
    />
}
